package com.ccdt.tv.smartrecommend.model.db.dao;

import android.content.Context;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * Created by syt on 2018/9/3.
 *
 */

public class DaoManager {
    private static  final String DB_NAME="sr_db";//数据库名称
    private volatile  static DaoManager manager;//防止多线程同时访问
    private  static DbHelper helper;
    private static  DaoMaster daoMaster;
    private static DaoSession daoSession;
    private Context context;

    /**
     * 使用单例模式获得操作数据库的对象
     * @return
     */
    public  static DaoManager getInstance(){
        if (manager==null){
            synchronized (DaoManager.class){
                if (manager==null){
                    manager = new DaoManager();
                }
            }
        }
        return manager;
    }

    public void init(Context context){
        this.context = context;
    }
    /**
     * 判断是否存在数据库，如果没有则创建数据库
     * @return
     */
    public DaoMaster getDaoMaster(){
        if (daoMaster==null){
            DbHelper helper = new DbHelper(context,DB_NAME,null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }

    /**
     * 完成对数据库的添加、删除、修改、查询的操作，仅仅是一个接口
     * @return
     */
    public DaoSession getDaoSession(){
        if (daoSession==null){
            if (daoMaster==null){
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    /**
     * 打开输出日志的操作,默认是关闭的
     */
    public void setDebug(){
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }

    /**
     * 关闭所有的操作,数据库开启的时候，使用完毕了必须要关闭
     */
    public void closeConnection(){
        closeHelper();
        closeDaoSession();
    }
    public  void  closeHelper(){
        if (helper!=null){
            helper.close();
            helper = null;
        }
    }
    public void closeDaoSession(){
        if (daoSession!=null){
            daoSession.clear();
            daoSession = null;
        }
    }
}

